﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace Nyamr.Implementation
{
    class MapperHost
    {
        public BaseMapper Mapper { get; set; }
        public string Target { get; set; }
        public string Source { get; set; }

        public void Execute() 
        {
            var input = Path.Combine(Source, "data");

            var emitter = new Emitter();
            emitter.NextID = 0;
            emitter.Target = Path.Combine(Target, "data");
            
            var output = new FileStream(Path.Combine(Target, "index.data"), FileMode.CreateNew);
            var log = new StreamWriter(Path.Combine(Target, "log.data"));

            emitter.Index = new StreamWriter(output);

            var index = new StreamReader(Path.Combine(Source, "index.data"), Encoding.UTF8);
            while (!index.EndOfStream)
            {
                var key = index.ReadLine();
                var path = Path.Combine(input, index.ReadLine());
                if (File.Exists(path))
                {
                    var file = new FileStream(path, FileMode.Open);
                    var value = new byte[file.Length];
                    file.Read(value, 0, value.Length);
                    file.Close();
                    try
                    {
                        log.WriteLine("Processing of '{0}' started  at {1}", key, DateTime.Now);
                        log.Flush();
                        Mapper.Map(key, value, emitter);
                        log.WriteLine("Processing of '{0}' finished at {1}", key, DateTime.Now);
                        log.Flush();
                    }
                    catch (Exception)
                    {
                        Console.WriteLine("Failed at '{0}' map task", path);
                        throw;
                    }
                }
                else 
                {
                    Console.WriteLine("There is no file for key = {0}", key);
                }
            }
            
            output.Flush();
            output.Close();
            log.Flush();
            log.Close();

            Mapper.Dispose();
        }

        public void GetStatistics() 
        {
            var input = Path.Combine(Source, "data");

            var emitter = new VoidEmitter();
            
            var index = new StreamReader(Path.Combine(Source, "index.data"), Encoding.UTF8);

            var buffer = new List<KeyValuePair<string, byte[]>>();

            Console.WriteLine("Loading of data to memory started at {0}", DateTime.Now);
            while (!index.EndOfStream)
            {
                var key = index.ReadLine();
                var path = Path.Combine(input, index.ReadLine());
                var file = new FileStream(path, FileMode.Open);
                var value = new byte[file.Length];
                file.Read(value, 0, value.Length);
                file.Close();
                buffer.Add(new KeyValuePair<string, byte[]>(key, value));
            }
            Console.WriteLine("Loaded finished at {0}", DateTime.Now);

            Console.WriteLine("Executing of tasks started at {0}", DateTime.Now);
            var tasks = 0;
            foreach (var task in buffer)
            {
                Mapper.Map(task.Key, task.Value, emitter);
                tasks++;
            }
            Console.WriteLine("Executing of tasks finished at {0}", DateTime.Now);
            Console.WriteLine("Total tasks count: {0}", tasks);

            Mapper.Dispose();
        }

        public void Execute(string key)
        {
            var input = Path.Combine(Source, "data");

            var emitter = new TestEmitter();

            var index = new StreamReader(Path.Combine(Source, "index.data"), Encoding.UTF8);
            while (!index.EndOfStream)
            {
                if (index.ReadLine() == key)
                {
                    var path = Path.Combine(input, index.ReadLine());
                    var file = new FileStream(path, FileMode.Open);
                    var value = new byte[file.Length];
                    file.Read(value, 0, value.Length);
                    file.Close();
                    Mapper.Map(key, value, emitter);
                }
                else index.ReadLine();
            }

            Mapper.Dispose();
        }
    }
}
